<!DOCTYPE html>
  <html>
  <head>
      <meta charset="utf-8">
      <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
      <title>发布商品</title>
      <link rel="stylesheet" type="text/css" href="../../css/api.css"/>
      <style>
          body{
            background-color: #fcfcfc;
            height:100%;
            padding-top:40px;
          }
          *{font-size:9pt;border:0;margin:0;padding:0;}
          body{font-family:'微软雅黑'; margin:0 auto;}
          ul,li{display:block;margin:0;padding:0;list-style:none;}
          .header{
            width: 100%;
            height:40px;
            background-color: #f5f5f5;
          }
          .leftreturn{
            width:25px;
            height:25px;
            margin:7.5px;
            float: left;
          }
          table{border-collapse:collapse;border-spacing: 0;}
          h2{font-weight:normal;}
          input{font-family:Tahoma,'微软雅黑','宋体';}
          textarea{border:1px solid #a7b5bc;width:500px;height:60px;}
          select{background:url(../../image/inputbg.gif) repeat-x;height:32px; border-top:solid 1px #a7b5bc; border-left:solid 1px #a7b5bc; border-right:solid 1px #ced9df; border-bottom:solid 1px #ced9df;padding:5px;}
          .floatl{float:left;}
          .floatr{float:right;}
          .sort{padding-left:3px;}
          .table1 tr td{padding:3px;}
          .table1 tr td b{ color:red;margin-left:5px;}
          /*form*/
          .formbody{padding:10px 10px;}
          .forminfo li{margin-bottom:13px; clear:both;}
          .forminfo li label{width:20%;line-height:25px; display:block; float:left;}
          .forminfo li i{color:#7f7f7f; padding-left:20px; font-style:normal;}
          .dfinput{width:70%; height:25px; line-height:25px; border-top:solid 1px #a7b5bc; border-left:solid 1px #a7b5bc; border-right:solid 1px #ced9df; border-bottom:solid 1px #ced9df;text-indent:10px;}
          .textinput{border-top:solid 1px #a7b5bc; border-left:solid 1px #a7b5bc; border-right:solid 1px #ced9df; border-bottom:solid 1px #ced9df; background:url(../../image/inputbg.gif) repeat-x; padding:10px; width:504px; height:135px; line-height:20px; overflow:hidden;}
          .btn{width:137px;height:35px; background:url(../../image/btnbg.png) no-repeat; font-size:14px;font-weight:bold;color:#fff; cursor:pointer;}

      </style>
  </head>
  <body>
    <div class="header">
      <img src="../../image/leftreturn.png" alt="" class="leftreturn" onclick="returnframe3();">
    </div>
        <div class="formbody">
              <ul class="forminfo">
                <li>
                  <label>商品缩略图</label>
                  <button onclick="showAction()" id="cimage">上传图片</botton>
                </li>
                <li>
                  <label>商品名称</label>
                  <br>
                  <input name="goodsName" id="goodsName" type="text" class="dfinput" value="请填写商品名称"
                  onfocus="if (value =='请填写商品名称'){value =''}" onblur="if (value ==''){value='请填写商品名称'}"
                    required="required" style="width:100%;min-height:30px;"/>
                </li>
                <li>
                  <label>商品类别</label>
                  <div class="vocation">
                    <select class="select3" name="goodsType" id="goodsType">
                      <option>土特产</option>
                      <option>美食外卖</option>
                      <option>超市商品</option>
                    </select>
                  </div>
                </li>
                <li>
                  <label>商品单价</label>
                  <input name="unitPrice" id="unitPrice" class="dfinput" type="number"  required="required"  style="width:100px;"/>元 </li>
                  <li>
                    <label>商品邮费</label>
                    <input name="postage" id="postage" class="dfinput" type="number"  required="required"  style="width:100px;"/>元 </li>
                <li>
                  <label>商品总量</label>
                  <input name="goodsNumber" id="goodsNumber" type="number" class="dfinput"  required="required" style="width:100px;"/>
                  件 </li>
                <li>
                  <label>商品描述</label>
                  <textarea name="goodsDescription" id="goodsDescription" rows="3" id="content"  style="width:100%;min-height:100px;"></textarea>
                </li>
                <li>  <label>&nbsp;</label>
                  <input type="submit" class="btn" id="btnPublish" value="马上发布" onclick="uploading()"/>
                </li>
              </ul>
        </div>
  </body>
  <script type="text/javascript" src="../../script/api.js"></script>
  <script type="text/javascript" src="../../script/SHA1.js"></script>
  <script type="text/javascript">
      //选择图片
function showAction() {
     api.actionSheet({
         title: '上传图片',
         cancelTitle: '取消',
         buttons: ['拍照', '从手机相册选择']
     }, function(ret, err) {
         if (ret) {
             getPicture(ret.buttonIndex);
         }
     });
 }
//从手机上传图片
 function getPicture(sourceType) {
    if (sourceType == 1) { // 拍照
        api.getPicture({
            sourceType: 'camera',
            encodingType: 'jpg',
            mediaValue: 'pic',
            allowEdit: false,
            destinationType: 'url',
            quality: 100,
            targetWidth: 200,
            targetHeight: 200,
            saveToPhotoAlbum: true
        }, function(ret, err) {
            if(ret){
                cimage=ret.data;
            }
        });
    } else if (sourceType == 2) { // 从相机中选择
        api.getPicture({
            sourceType: 'library',
            encodingType: 'jpg',
            mediaValue: 'pic',
            destinationType: 'url',
            quality: 100,
            targetWidth: 200,
            targetHeight: 200
        }, function(ret, err) {
            if(ret){
                cimage=ret.data;
            }
        });
    }else{
        return;
    }
}

function uploading(){
  user =$api.getStorage("user");
  goodsName=document.getElementById("goodsName").value;
  goodsType=document.getElementById("goodsType").value;
  unitPrice=document.getElementById("unitPrice").value;
  postage=document.getElementById("postage").value;
  goodsNumber=document.getElementById("goodsNumber").value;
  goodsDescription=document.getElementById("goodsDescription").value;

  if(goodsType=="土特产"){
    goodsType="ttc";
  }else if (goodsType=="美食外卖") {
    goodsType="mswm";
  }else if (goodsType=="超市商品") {
    goodsType="csyp";
  }

  //获取X-APICloud-AppKey值
  var now = Date.now();
 appKey = SHA1("A6032422409194"+"UZ"+"808CF36B-60D4-DC5D-C9F3-FB49F9E1FB44"+"UZ"+now)+"."+now

  api.ajax({
    url: 'https://d.apicloud.com/mcm/api/file',
    method: 'post',
    headers:{
      "X-APICloud-AppId": "A6032422409194",
      "X-APICloud-AppKey": appKey
    },
      data: {
          files: {
              file: cimage
          }
      }
  },function(ret, err){
      if (ret) {
          $api.setStorage('image', ret);
      } else {
          alert("数据库连接失败");
      }
  });

  image = $api.getStorage('image');
  var file={
      id: image.id,
      name: image.name,
      url: image.url
}

api.ajax({
  url:"https://d.apicloud.com/mcm/api/user/"+user[0].id+"/commodity",
  method: 'post',
  headers:{
    "X-APICloud-AppId": "A6032422409194",
    "X-APICloud-AppKey": appKey
  },
  data: {
      values: {//上传参数
        "cimage":file,
        "cname":goodsName,
        "variety":goodsType,
        "cprice":unitPrice,
        "postage":postage,
        "amount":goodsNumber,
        "introduce":goodsDescription
      }
  }
},function(ret, err){
    if (ret) {
          alert("发布成功");
          commodity=$api.getStorage("commodity");
          commodity[commodity.length]=ret;
          api.sendEvent({
              name: 'updateCommity',
              extra: {
                  Commity: commodity
              }
          });


          returnframe3()
    } else {
        alert("数据库连接失败");
    }
});
}


      function returnframe3() {
        api.closeWin();
      }
  </script>
  </html>
